import { defineStore } from 'pinia'

import { getRecommendApi, getImgurlApi } from '@/api/home/getRecommendApi'
import type { HomeStoreDataType, NewSongListType } from './homeStoreType'

const initState = {
  recomPlaylist: [],
  newSongList: [],
  newSongLanList: [],
  lan: '',
  wonderful: [],
  new_album: [],
  toplist: [],
}
export const useHomeStore = defineStore('homeStore', {
  state: (): HomeStoreDataType => initState,
  actions: {
    async getAllHomeData() {
      const res = await getRecommendApi()
      console.log(res)
      this.recomPlaylist = res.recomPlaylist.data.v_hot
      this.lan = res.new_song.data.lan
      this.newSongLanList = res.new_song.data.lanlist
      this.newSongList = res.new_song.data.songlist.map((item) => ({
        id: item.id,
        type: item.type,
        mid: item.album.mid,
        name: item.name,
        title: item.title,
        singer: item.singer,
        songMid: item.mid,
      }))
      this.newSongList.forEach(async (song) => {
        song.imgUrl = (await getImgurlApi(song.mid)).data.imageUrl
      })
      this.wonderful = res.focus.data.content.map((item) => ({
        id: item.id,
        picInfo: item.pic_info.url,
      }))
      this.new_album = res.new_album.data.albums.map((item) => ({
        id: item.id,
        name: item.name,
        photo: item.photo.pic_mid,
        singers: item.singers,
        mid: item.mid,
      }))
      this.new_album.forEach(async (alb) => {
        alb.photo = (await getImgurlApi(alb.photo)).data.imageUrl
      })
      this.toplist = res.toplist.data.group[0].toplist
        .map((top) => ({
          song: top.song,
          title: top.title,
          id: top.topId,
        }))
        .slice(0, 5) // 只展示前5个
    },
  },
})
